package cn.doctorpda.conference.presenter;

import org.json.JSONException;
import org.json.JSONObject;

import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.checkmeeting.DataProvinceInfo;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.config.NetConfig;
import cn.doctorpda.conference.model.GetProvinceModel;
import cn.doctorpda.conference.model.IPostModel;
import cn.doctorpda.conference.okhttp.callback.StringCallback;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.GsonUtil;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.view.callbackview.IProvinceView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/19.
 */
public class GetProvincePresenter {
    private IPostModel model;
    private IProvinceView provinceView;
    private DataProvinceInfo info = null;

    public GetProvincePresenter(IProvinceView baseView) {
        model = new GetProvinceModel();
        provinceView = (IProvinceView) baseView;
    }

    public void getProvinceData() {
        model.postNetWorkStringData(NetConfig.GET_PROVINCE, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                provinceView.setResultError(call, e);
            }

            @Override
            public void onResponse(String response) {
                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);

                    if (jsonObject.getString("encrpt_flag").equals("1")) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, DataProvinceInfo.class);
                    } else {
                        info = GsonUtil.fromJson(msg.getData(), DataProvinceInfo.class);
                    }
                    provinceView.setProvinceView(msg, info);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
